package com.wugh.fast.security.social.config;

import com.wugh.fast.security.social.app.qq.api.QQApi;
import com.wugh.fast.security.social.app.wechat.api.WechatApi;
import com.wugh.fast.security.social.service.QQAutoRegister;
import com.wugh.fast.security.social.service.WechatAutoRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionSignUp;

/**
 * 说明:
 *
 * @author wugh
 * @date 2018-09-13 17:55
 */
public class AutoConnectionSignUp implements ConnectionSignUp {
    @Lazy
    @Autowired(required = false)
    private QQAutoRegister qqAutoRegister;
    @Lazy
    @Autowired(required = false)
    private WechatAutoRegister wechatAutoRegister;

    @Override
    public String execute(Connection<?> connection) {
        Object api = connection.getApi();
        if (api instanceof QQApi && qqAutoRegister != null) {
            return qqAutoRegister.register(((QQApi) api).getUserInfo());
        } else if (api instanceof WechatApi && wechatAutoRegister != null) {
            return wechatAutoRegister.register(((WechatApi) api).getUserInfo(connection.getKey().getProviderUserId()));
        }
        return null;
    }
}
